/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.woundedwolf.android.tumbleweed;

import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import com.woundedwolf.android.tumbleweed.elements.Renderable;
import java.util.ArrayList;
import java.util.List;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 *
 * @author FlameDuck
 */
public class Engine implements GLSurfaceView.Renderer
{
	private boolean ready;
	private int graphicsWidth;
	private int graphicsHeight;
	private GLSurfaceView view;
	private List<Renderable> renderables;

	Engine(GLSurfaceView view)
	{
		this.view = view;
		ready = false;
		renderables = new ArrayList<Renderable>();
	}

	public int graphicsWidth()
	{
		return graphicsWidth;
	}

	public int graphicsHeight()
	{
		return graphicsHeight;
	}

	public void onDrawFrame(GL10 glContext)
	{
		glContext.glClear(GL10.GL_COLOR_BUFFER_BIT);
		glContext.glLoadIdentity();
		GLU.gluOrtho2D(glContext, 0, graphicsWidth, graphicsHeight, 0);

		for (Renderable renderable : renderables) {
			renderable.draw(glContext);
		}
	}

	public void onSurfaceChanged(GL10 glContext, int width, int height)
	{
		ready = false;
		glContext.glViewport(0, 0, width, height);

		float ratio = (float) width / height;
		graphicsHeight = height;
		graphicsWidth = width;

		glContext.glMatrixMode(GL10.GL_PROJECTION);
		ready = true;
	}

	public void onSurfaceCreated(GL10 glContext, EGLConfig config)
	{
		glContext.glDisable(GL10.GL_DITHER);
		glContext.glDisable(GL10.GL_DEPTH_TEST);
		glContext.glEnable(GL10.GL_POINT_SMOOTH);

		glContext.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST);

		glContext.glClearColor(0, 0, 0, 1);
		glContext.glShadeModel(GL10.GL_SMOOTH);
	}

	public int getGraphicsHeight()
	{
		return graphicsHeight;
	}

	public int getGraphicsWidth()
	{
		return graphicsWidth;
	}

	public GLSurfaceView getView()
	{
		return view;
	}

    public void addRenderable(Renderable renderable)
    {
		renderables.add(renderable);
    }

    public void removeRenderable(Renderable renderable)
    {
		renderables.remove(renderable);
    }

	public boolean isReady()
	{
		return ready;
	}
}
